package com.songcha.library_common.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.TextView
import com.songcha.library_common.R
import com.songcha.library_common.util.dp2px




class LoadingDialog(val mContext:Context) : Dialog(mContext) {
    private var mWidth:Int?=null
    private var mHeight:Int?=null
    private var mGravity:Int=Gravity.CENTER
    private var mTitle:String=""
    private var mMsg:String=""
    private var mConfirmListener:(()->Unit)?=null
    private var mProgress=0
    private lateinit var mTvProgress:TextView

    override fun onCreate(savedInstanceState: Bundle?) {

       // window?.attributes?.width=200f.dp2px().toInt()


        setContentView(R.layout.common_dialog_loading)
        mTvProgress=findViewById(R.id.common_dialog_tv_progress)

        init()

    }

    fun setProgress(progress:Int){
        if(mProgress==progress) return
        if(progress<0) mProgress=0
        else if(progress>100) mProgress=100
        else mProgress=progress
        updateProgress()
    }

    fun updateProgress(){
        mTvProgress.apply {
            if(mProgress>0){
                visibility= View.VISIBLE
                text=mProgress.toString()+"%"
            }else{
                visibility= View.GONE
                text=""
            }
        }
    }





    private fun init(){

        window?.attributes?.apply {
            width = if(mWidth!=null){
                mWidth!!
            }else{
                100f.dp2px().toInt()
            }

          /*  height = if(mHeight!=null){
                mHeight!!
            }else{
                200f.dp2px().toInt()
            }*/
            /*if(mHeight!=null)
                height=mHeight!!*/

            height=100f.dp2px().toInt()

            alpha=1f


       }

        //设置背景透明，否则圆角没有效果
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        window?.setGravity(mGravity)


        //window?.setWindowAnimations(R.style.dialogWindowAnim)






    }






}